import { OG } from "./CommonClass";
import { GlobalClass } from "./GlobalClass";
/**通用方法类 */
export class CommonFunction {

    private static self: CommonFunction;

    public static ins() {
        if (CommonFunction.self != null) {
            return CommonFunction.self;
        } else {
            return new CommonFunction();
        }
    }

    public constructor() {
        CommonFunction.self = this;
    }

    /**根据id获取HTML元素
     * @param id_name 元素id名称
    */
    public IdToElement(id_name: string) {
        let temp_ele: HTMLElement = document.getElementById(id_name);
        return temp_ele;
    }

    /**使元素居中[margin]
     * @param elem 要居中的元素
     * @param refer 居中参照的宽高
     * @param dire 居中的方向 H:横向居中 V:纵向居中 HV:横向纵向都居中
    */
    public CenterElement(elem: HTMLElement, refer: OG.Size, dire: string) {
        let H_Interval: number = 0;
        let V_Interval: number = 0;
        let elem_width: number = parseInt(elem.style.width.replace(" px",""));
        let elem_height: number = parseInt(elem.style.height.replace(" px",""));
        if(isNaN(elem_width) && dire == "H") {
            return;
        }else if(isNaN(elem_height) && dire == "V") {
            return;
        }else if(isNaN(elem_width) || isNaN(elem_height)) {
            return;
        }
        H_Interval = (refer.width - elem_width) / 2;
        V_Interval = (refer.height - elem_height) / 2;
        switch(dire) {
            case "H":
                elem.style.marginLeft = H_Interval + "px";
                elem.style.marginRight = H_Interval + "px";
            break;
            case "V":
                elem.style.marginTop = V_Interval + "px";
                elem.style.marginBottom = V_Interval + "px";
            break;
            case "HV":
                elem.style.marginLeft = H_Interval + "px";
                elem.style.marginRight = H_Interval + "px";
                elem.style.marginTop = V_Interval + "px";
                elem.style.marginBottom = V_Interval + "px";
            break;
        }
    }

    /**两点之间的距离 */
    public InstanceToPoints (A:OG.Positon,B:OG.Positon) {
        let ins: number = 0;
        let double_x: number = (A.x - B.x) * (A.x - B.x);
        let double_y: number = (A.y - B.y) * (A.y - B.y);
        ins = Math.abs(Math.sqrt(double_x + double_y));
        return parseInt(ins.toFixed(0));
    }

    /**往对象内添加新属性 
     * @param add_obj 要添加的对象
     * @param map_atts 要添加的属性类型与值,以map类型的数组形式
    */
    public AddObjectAttribute(add_obj:Object,map_atts:Map<string,any>[]) {
        if(typeof add_obj == "object") {
            for(let i = 0; i < map_atts.length; i++) {
                let temp_map:Map<string,any> = map_atts[i];
                temp_map.forEach((value:string,key:any) => {
                    add_obj[key] = value;
                });
            }
        }
    }

    /**多种标签监听同类型的事件
     * @param elems 要监听的元素数组
     * @param event 要监听的事件
     * @param callback 监听回调
    */
    public ListenerTypeEvents(elems:any[],event:string,callback:Function) {
        let temp:any = null;
        if(elems.length != 0) {
            for(let i = 0; i < elems.length; i++) {
                temp = elems[i];
                GlobalClass.ListenerCenter.addEvent(event,temp,callback);
            }
        }
    }
}